#include "SysTray.h"
#include <QIcon>

SysTray::SysTray(QWidget *parent)
	: QSystemTrayIcon(parent), m_parent(parent)
{
	initSystemTray();
	show();

	connect(this, &QSystemTrayIcon::activated,
	[parent, this](QSystemTrayIcon::ActivationReason reason)
	{
		if (reason == QSystemTrayIcon::Trigger) {
			if (parent->isHidden()) 
			{
				parent->show();
				parent->activateWindow();
			}
			else
			{
				parent->hide();
			}
			
		}
		else if (reason == QSystemTrayIcon::Context)
		{
			this->addSystrayMenu();
		}
	});

	//addSystrayMenu();
	//addSystrayMenu();
}

SysTray::~SysTray()
{
	hide();
}

void SysTray::initSystemTray()
{
	setIcon(QIcon(":/Resources/MainWindow/app/logo.ico"));

	setToolTip("QQ");//托盘提示
}

void SysTray::addSystrayMenu()
{
	//CustomMenu * menus = new CustomMenu(qobject_cast<QWidget*>(this));
	CustomMenu * menus = new CustomMenu(m_parent);

	menus->addCustomMenu("show", ":/Resources/MainWindow/app/logo.ico");
	menus->addCustomMenu("quit", ":/Resources/MainWindow/app/page_close_btn_hover.png");

	connect(menus->getAction("show"),
		SIGNAL(triggered(bool)),
		m_parent,
		SLOT(onShowNormal(bool)));
	connect(menus->getAction("quit"),
		SIGNAL(triggered(bool)),
		m_parent,
		SLOT(onShowQuit(bool)));
	
	menus->exec(QCursor::pos());
	//menus->show();

	delete menus;
	menus = nullptr;
}
